﻿Imports EntitiesLayer
Imports BLL

Public Class FrmMantRoles
    Private validador As ValidadorDeFormularios
    Private modoEsInsertar As Boolean = True
    Private ListaRoles As IEnumerable(Of Rol)
    Private listaPermisos As IList(Of EntitiesLayer.Permiso)

    Private Sub bntSalir_Click(sender As Object, e As EventArgs) Handles bntSalir.Click
        Me.Close()
    End Sub

    Private Sub FrmMantRoles_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ModuloGeneral.Centrar(pnPrincipal)

       
        ListaRoles = ModuloGeneral.gestorRol.ListarRoles()
        listaPermisos = ModuloGeneral.gestorRol.ListarPermisos()
        agregarPermiso(listaPermisos)
        agregarRol(ListaRoles)
        validador = New ValidadorDeFormularios(Me, ePMantenimientoRoles)
        lbTodosPErmisos.DrawMode = DrawMode.OwnerDrawVariable
        LbPermisosDeRol.DrawMode = DrawMode.OwnerDrawVariable
        lbRoles.DrawMode = DrawMode.OwnerDrawVariable
        gbRegistrar.Location = New Point((gbRegistrar.Parent.Width - gbRegistrar.Width) / 2, gbRegistrar.Location.Y)
        validarPermisos()
    End Sub

    Private Sub validarPermisos()
        If (Not ModuloGeneral.gestorProcesos.esPermitido(Permiso.Listar_roles)) Then

            txtbusquedaRol.Enabled = False
            lbRoles.Items.Clear()
            btnUpdate.Enabled = False
            btnPermisosAsociados.Enabled = False
            lbRoles.Enabled = False
            LbPermisosDeRol.Enabled = False
            btnRemover.Enabled = False


        End If

        If (Not ModuloGeneral.gestorProcesos.esPermitido(Permiso.Eliminar_roles)) Then
            btnEliminarRol.Enabled = False

        End If
        If (Not ModuloGeneral.gestorProcesos.esPermitido(Permiso.Incluir_roles)) Then

            txtStrNombreRol.Enabled = False
            btnAceptar.Enabled = False
            btnIncluirRol.Enabled = False

        End If

        If (Not ModuloGeneral.gestorProcesos.esPermitido(Permiso.Modificar_roles)) Then

            btnModificarRol.Enabled = False
            btnAceptarPermiso.Enabled = False
            txtStrPermiso.Enabled = False
        End If
        


    End Sub


    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click

        ListaRoles = ModuloGeneral.gestorRol.ListarRoles()

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnEliminarRol.Click
        If (Not lbRoles.SelectedItem Is Nothing) Then
            Dim action = Sub()
                             Dim rol = DirectCast(lbRoles.SelectedItem, Rol)
                             ModuloGeneral.gestorRol.EliminarRol(rol)

                             lbRoles.Items.Remove(rol)
                         End Sub
            ModuloGeneral.intercept.InterSept("El rol se elimino con exito", action)

        End If

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles btnAsignar.Click
        If (lbRoles.SelectedItem Is Nothing Or lbTodosPErmisos.SelectedItem Is Nothing) Then

            MsgBox("Seleccioné un rol y un permiso para poder asignar")

        Else
            Dim idRol = DirectCast(lbRoles.SelectedItem, Rol).Id
            Dim idPermiso = DirectCast(lbTodosPErmisos.SelectedItem, EntitiesLayer.Permiso).Id
            Dim action = Sub()
                             ModuloGeneral.gestorRol.asignarRolPermiso(idRol:=idRol, idPermiso:=idPermiso)
                         End Sub

            ModuloGeneral.intercept.InterSept("El permiso se asignó con exito", action)
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnPermisosAsociados.Click
        If (Not lbRoles.SelectedItem Is Nothing) Then
            Dim idRol = DirectCast(lbRoles.SelectedItem, Rol).Id
            LbPermisosDeRol.DataSource = Nothing
            LbPermisosDeRol.DataSource = ModuloGeneral.gestorRol.PermisoDeRol(idRol)
            ModuloGeneral.animator.ShowClip(LbPermisosDeRol)
        End If
    End Sub

    Private Sub lbRoles_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lbRoles.SelectedIndexChanged

        LbPermisosDeRol.DataSource = Nothing
        If (Not modoEsInsertar) Then
            txtStrNombreRol.Text = ""
            Dim rol = DirectCast(lbRoles.SelectedItem, Rol)
            If (Not rol Is Nothing) Then
                txtStrNombreRol.Text = rol.Nombre
            End If
        End If
    End Sub


    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles btnAceptar.Click
        validador.Control = gbRegistrar
        If (validador.ValidarFormulario()) Then
            If (modoEsInsertar) Then


                Dim action = Sub()
                                 ModuloGeneral.gestorRol.registrarRol(txtStrNombreRol.Text)

                                 ListaRoles = ModuloGeneral.gestorRol.ListarRoles()
                                 lbRoles.Items.Clear()
                             End Sub

                ModuloGeneral.intercept.InterSept("El rol se registró con exito", action)


            ElseIf (Not lbRoles.SelectedItem Is Nothing) Then
                Dim idRol = DirectCast(lbRoles.SelectedItem, Rol).Id
                Dim action = Sub()
                                 ModuloGeneral.gestorRol.ModificarRol(idRol:=idRol, nombre:=txtStrNombreRol.Text)
                               

                                 ListaRoles = ModuloGeneral.gestorRol.ListarRoles()
                                 lbRoles.Items.Clear()
                             End Sub

                ModuloGeneral.intercept.InterSept("El rol se modifico con exito con exito", action)
            End If

        End If

    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click


        listaPermisos = ModuloGeneral.gestorRol.ListarPermisos()
    End Sub

    Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles lbTodosPErmisos.DrawItem
        If (lbTodosPErmisos.Items.Count > 0) Then
            e.DrawBackground()
            e.Graphics.DrawString(lbTodosPErmisos.Items(e.Index).ToString, lbTodosPErmisos.Font, Brushes.Black, e.Bounds.Left, ((e.Bounds.Height - lbTodosPErmisos.Font.Height) \ 2) + e.Bounds.Top)
        End If
    End Sub

    Private Sub ListBox1_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles lbTodosPErmisos.MeasureItem
        e.ItemHeight = 30
    End Sub
    Private Sub ListBox2_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles LbPermisosDeRol.DrawItem
        If (LbPermisosDeRol.Items.Count > 0) Then
            e.DrawBackground()
            e.Graphics.DrawString(LbPermisosDeRol.Items(e.Index).ToString, LbPermisosDeRol.Font, Brushes.Black, e.Bounds.Left, ((e.Bounds.Height - LbPermisosDeRol.Font.Height) \ 2) + e.Bounds.Top)
        End If
    End Sub

    Private Sub ListBox2_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles LbPermisosDeRol.MeasureItem
        e.ItemHeight = 30
    End Sub
    Private Sub ListBox3_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles lbRoles.DrawItem
        If (lbRoles.Items.Count > 0) Then
            e.DrawBackground()
            e.Graphics.DrawString(lbRoles.Items(e.Index).ToString, lbRoles.Font, Brushes.Black, e.Bounds.Left, ((e.Bounds.Height - lbRoles.Font.Height) \ 2) + e.Bounds.Top)
        End If
    End Sub

    Private Sub ListBox3_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles lbRoles.MeasureItem
        e.ItemHeight = 30
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles btnRemover.Click
        If (Not LbPermisosDeRol.SelectedItem Is Nothing And Not lbRoles.SelectedItem Is Nothing) Then
            Dim action = Sub()

                             Dim permiso = DirectCast(LbPermisosDeRol.SelectedItem, EntitiesLayer.Permiso)
                             Dim rol = DirectCast(lbRoles.SelectedItem, Rol)
                             ModuloGeneral.gestorRol.RemoverRolPermiso(rol.Id, permiso.Id)
                             Dim list = DirectCast(LbPermisosDeRol.DataSource, IList(Of EntitiesLayer.Permiso))
                             list.Remove(permiso)
                             LbPermisosDeRol.DataSource = Nothing
                             LbPermisosDeRol.DataSource = list
                         End Sub
            ModuloGeneral.intercept.InterSept("El permiso se removió con exito", action)

        End If
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles btnIncluirRol.Click
        txtStrNombreRol.Text = ""
        modoEsInsertar = True
        btnAsignar.Visible = False
        btnPermisosAsociados.Visible = False
        pnHide.Visible = False
        ModuloGeneral.animator.Move(gbRegistrar, (gbRegistrar.Parent.Width - gbRegistrar.Width) / 2, gbRegistrar.Location.Y)
        gbRegistrar.Text = "Registrar rol"
    End Sub

    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles btnModificarRol.Click
        btnAceptar.Enabled = True
        txtStrNombreRol.Enabled = True
        modoEsInsertar = False

        ModuloGeneral.animator.Move(gbRegistrar, gbRegistrar.Parent.Location.X + 10, gbRegistrar.Location.Y)
        btnAsignar.Visible = True
        btnPermisosAsociados.Visible = True
        pnHide.Visible = True
        gbRegistrar.Text = "Modificar rol"

    End Sub

    Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles btnAceptarPermiso.Click
        validador.Control = gbModificarPermiso
        If (validador.ValidarFormulario()) Then
            Dim Permiso = DirectCast(lbTodosPErmisos.SelectedItem, EntitiesLayer.Permiso)

            If (Not Permiso Is Nothing) Then

                Dim action = Sub()
                                 ModuloGeneral.gestorRol.ModificarPermiso(Permiso.Id, txtStrPermiso.Text)


                                 listaPermisos = ModuloGeneral.gestorRol.ListarPermisos()
                                 lbTodosPErmisos.Items.Clear()
                             End Sub
                ModuloGeneral.intercept.InterSept("El permiso se modificó con exito", action)


            End If

        End If
    End Sub

    Private Sub lbTodosPErmisos_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lbTodosPErmisos.SelectedIndexChanged
        Dim permiso = DirectCast(lbTodosPErmisos.SelectedItem, EntitiesLayer.Permiso)
        If (Not permiso Is Nothing) Then
            txtStrPermiso.Text = permiso.Nombre
        End If

    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles txtBusquedaPermiso.TextChanged
        lbTodosPErmisos.Items.Clear()
        If (txtBusquedaPermiso.Text.Equals("")) Then

            agregarPermiso(listaPermisos)
        Else
            agregarPermiso(listaPermisos.Like(Function(p) p.Nombre + p.DescTransaccion, txtBusquedaPermiso.Text))
        End If
    End Sub

    Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles txtbusquedaRol.TextChanged
        lbRoles.Items.Clear()
        If (txtbusquedaRol.Text.Equals("")) Then


            agregarRol(ListaRoles)

        Else
            agregarRol(ListaRoles.Like(Function(p) p.Nombre, txtbusquedaRol.Text))
        End If
    End Sub

    Public Sub agregarPermiso(list As IEnumerable(Of EntitiesLayer.Permiso))

        For Each p As EntitiesLayer.Permiso In list
            lbTodosPErmisos.Items.Add(p)
        Next
    End Sub

    Public Sub agregarRol(list As IEnumerable(Of Rol))

        For Each p As Rol In list
            lbRoles.Items.Add(p)
        Next
    End Sub

End Class